/*
 * Copyright (C) 2009 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.collections;

import name.kudryashov.annotations.Immutable;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Arrays;

/**
 * Tuple of three elements.
 * @param <T1> type of first element
 * @param <T2> type of second element
 * @param <T3> type of third element
 * <p>
 * Implements {@link Iterable} allowing to iterate over tuple's members as over objects.
 */
@Immutable
public class Tuple3<T1, T2, T3> implements Serializable, Iterable<Object> {

    private final T1 first;
    private final T2 second;
    private final T3 third;

    public Tuple3(T1 first, T2 second, T3 third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    /**
     * @return first member of the tuple
     */
    public T1 get1() {
        return first;
    }

    /**
     * @return second member of the tuple
     */
    public T2 get2() {
        return second;
    }

    /**
     * @return third member of the tuple
     */
    public T3 get3() {
        return third;
    }

    /**
     * @return first two members as a 2-element tuple
     */
    public Tuple2<T1, T2> get12() {
        return new Tuple2<T1,T2>(first, second);
    }

    /**
     * @return second and third members as a 2-element tuple
     */
    public Tuple2<T2, T3> get23() {
        return new Tuple2<T2,T3>(second, third);
    }

    /**
     * @return first and third members as a 2-element tuple
     */
    public Tuple2<T1, T3> get13() {
        return new Tuple2<T1,T3>(first, third);
    }

    public Iterator<Object> iterator() {
        return Arrays.<Object>asList(first, second, third).iterator();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tuple3 tuple3 = (Tuple3) o;

        return !(first != null ? !first.equals(tuple3.first) : tuple3.first != null) &&
               !(second != null ? !second.equals(tuple3.second) : tuple3.second != null) &&
               !(third != null ? !third.equals(tuple3.third) : tuple3.third != null);
    }

    @Override
    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        result = 31 * result + (third != null ? third.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "(" + first + ", " + second + ", " + third + ")";
    }


    private static final long serialVersionUID = 7677330408254686370L;
}